package com.wolfpire.section.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.wolfpire.chapter.dao.ChapterDao;
import com.wolfpire.knowledge.dao.KnowledgeDao;
import com.wolfpire.section.dao.SectionDao;
import com.wolfpire.subject.dao.SubjectDao;
import com.wolfpire.system.common.Constants;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.base.dao.impl.BaseHibernateDao;
import com.wolfpire.system.dao.UserDao;
import com.wolfpire.system.model.Chapter;
import com.wolfpire.system.model.DataDict;
import com.wolfpire.system.model.Knowledge;
import com.wolfpire.system.model.Profession;
import com.wolfpire.system.model.Section;
import com.wolfpire.system.model.Subject;
import com.wolfpire.system.model.User;

@Repository("sectionDao")
public class SectionDaoImpl extends BaseHibernateDao<Section, Long> implements SectionDao {
	
	

	public List<Section> list(Section section) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page<Section> findSections(Page<Section> page, Section filterSection) {
		StringBuffer querySqlBuffer = new StringBuffer();
		StringBuffer countSqlBuffer = new StringBuffer();
		List<String> propertyNames = new ArrayList<String>();
		List propertyValues = new ArrayList();
		//querySqlBuffer.append(" SELECT u.* FROM t_sm_user u, t_sm_org o, t_sm_user_org uo ")
		querySqlBuffer.append(" SELECT s.* FROM t_section s  ")
				.append(" LEFT JOIN t_chapter_section cs ON s.id = cs.section_id  ")
				.append(" LEFT JOIN t_chapter_section c ON c.id = cs.chapter_id ")
				.append(" LEFT JOIN t_subject_chapter sc ON sc.chapter_id = cs.chapter_id ")
				.append(" WHERE 1 =1 ");
				//.append(" WHERE u.id = uo.user_id AND uo.org_id = o.id ");
		if (null != filterSection.getId()) {
			querySqlBuffer.append(" s.id =:id");
			propertyNames.add("id");
			propertyValues.add(filterSection.getId());
		}
		
		if (!StringUtils.isBlank(StringUtils.stripToEmpty(filterSection.getName()))) {
			querySqlBuffer.append(" AND k.name LIKE :name ");
			propertyNames.add("name");
			propertyValues.add("%" + StringUtils.stripToEmpty(filterSection.getName()) + "%");
		}
		
		if (null != filterSection.getChapterId()) {
			querySqlBuffer.append(" AND cs.chapter_id= :chapterId ");
			propertyNames.add("chapterId");
			propertyValues.add(filterSection.getChapterId());
		}
		
		if (null != filterSection.getSubjectId()) {
			querySqlBuffer.append(" AND sc.subject_id =:subjectId ");
			propertyNames.add("professionId");
			propertyValues.add(filterSection.getSubjectId());
		}
		
		countSqlBuffer.append(" SELECT count(1) from (").append(querySqlBuffer).append(") AS TOTAL ");
		int totalCount = this.findSqlInt(countSqlBuffer.toString(), propertyNames, propertyValues);
		page.setTotalCount(totalCount);
		querySqlBuffer.append(" ORDER BY s.id");
		if (0 < totalCount) {
			List<Section> result = this.setPageParameter(page, Section.class, querySqlBuffer.toString(), propertyNames, propertyValues);
			page.setDataList(result);
		}
		
		return page;
	}

	public Section getByName(String name) {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * 根据章Id获取底下的所有节
	 */
	public List<Section> getByChapterId(long chapterId) {
		StringBuffer querySql = new StringBuffer();
		List<String> propertyNames = new ArrayList<String>();
		List propertyValues = new ArrayList();
		querySql.append(" select s.* from t_section s,t_chapter c,t_chapter_section cs ");
		querySql.append(" where s.id = cs.section_id and c.id = cs.chapter_id and c.id = :chapterId  order by s.id");
		propertyNames.add("chapterId");
		propertyValues.add(chapterId);
		SQLQuery query = this.createSqlQuery(querySql.toString(), propertyNames, propertyValues);
		query.addEntity(Section.class);
		return query.list();
	}
}
